﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EmitMapper;
using EmitMapper.MappingConfiguration;
using EsayUIWebApplication1.Models;
using Tin.Esay.Framework.BO;
using Tin.Esay.Framework.Facade;
using Tin.Esay.Framework.Common.Utility;

namespace EsayUIWebApplication1.Controllers
{
    public class HomeController : BaseController
    {
        private OperationLogFacade _operationLogFacade;

        public HomeController()
        {
            _operationLogFacade = new OperationLogFacade();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult First()
        {
            return View();
        }

        public ActionResult Memu()
        {
            var result = new List<MenuModel>();
            var menuModel = new MenuModel()
            {
                id = 888,
                text = "hello world",
                url = "/home/first"
            };
            result.Add(menuModel);
            menuModel = new MenuModel()
            {
                id = 999,
                text = "基础",
                url = "/home/second"
            };
            result.Add(menuModel);
            menuModel = new MenuModel()
            {
                id = 1000,
                text = "开发规范",
                children = new List<MenuModel>()
                {
                    new MenuModel()
                    {
                        id = 1001,
                        text = "教材1",
                        url = "/home/jiaocai1"
                    },
                    new MenuModel()
                    {
                        id = 1002,
                        text = "教材2",
                        url = "/home/jiaocai2"
                    },
                    new MenuModel()
                    {
                        id = 1003,
                        text = "教材3",
                        url = "/home/jiaocai3"
                    }
                }
            };
            result.Add(menuModel);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult GetData(PageModel pageModel)
        {
            var orderModels = new List<OrderModel>();
            for (int i = pageModel.RowBegin; i <= pageModel.RowEnd; i++)
            {
                var orderModel = new OrderModel()
                {
                    CreateDate = DateTime.Now.ToString("yyyy-M-d dddd"),
                    Flag = 1,
                    Funding = "金驹" + i,
                    Number = new Random(Guid.NewGuid().GetHashCode()).Next(100),
                    OrderNo = "XS201506161032" + new Random(Guid.NewGuid().GetHashCode()).Next(100, 999),
                    Supplier = new SupplierModel()
                    {
                        Id = 1003,
                        Name = "供应商" + i
                    },
                    TotalQty = new Random(Guid.NewGuid().GetHashCode()).Next(100, 1000),
                    Unit = "aa"
                };
                orderModels.Add(orderModel);
            }

            var result = new { total = 300, rows = orderModels };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult OrderList(string orderNo, PageModel pageModel)
        {
            var orderModels = new List<OrderModel>();
            for (int i = pageModel.RowBegin; i <= pageModel.RowEnd; i++)
            {
                var orderModel = new OrderModel()
                {
                    OrderNo = orderNo ,
                    TotalQty = new Random(Guid.NewGuid().GetHashCode()).Next(100, 1000),
                };
                orderModels.Add(orderModel);
            }

            var result = new { total = 300, rows = orderModels };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult GetOperationLog()
        {
            var result = _operationLogFacade.GetOperationLogList();

            var operationModels = result.MapperDateHandle<OperationLogBO, OperationLogViewModel>();

            return JsonResult(operationModels);
        }
    }
}